package mouseEvents;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashSet;
import java.util.Random;

import javax.swing.JPanel;

public class PointPanel extends JPanel {
	/**
	 * 
	 */
	int n=0;
	private static final long serialVersionUID = 1L;

	private Point last;

    private static final Color xorColor = Color.cyan;
    private static final Color paintColor = Color.blue;
     private HashSet<Point> point;
     ImageC img;
    private volatile boolean unexpectedRepaints = false;

    public PointPanel() {
    	img = new ImageC();
        MouseHandler mh = new MouseHandler();
        addMouseListener(mh);
        addMouseMotionListener(mh);
        setBackground(Color.white);
        point = new HashSet<Point>();
        Thread t = new Thread(new Runnable() {
            Random r = new Random(System.currentTimeMillis());

            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(r.nextInt(500) + 100);
                    } catch (InterruptedException ex) {
                        ex.printStackTrace();
                        return;
                    }
                    if (unexpectedRepaints) {
                    	point.clear();
                    	PointPanel.this.repaint();
                    }
                }
            }
        }, "Unexpected repaints thread");
        t.setDaemon(true);
        t.start();
    }

    public void setUnexpectedRepaints(boolean value) {
        unexpectedRepaints = value;
    }


    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        
       
		img.paintComponent(g);
		
        
        
        
        Graphics2D g2d = (Graphics2D) g;
        Paint p = g2d.getPaint();
        g2d.setColor(paintColor);
    
        for(Point p1:point){
        	++n;
		
  			g2d.fillOval((int)p1.x-10,(int) p1.y-10, 20, 20);
			
    	}
       if(n>=Graph.Graph.maxVertexes){
    	   DrawFrame.moreMany();
       }
        g2d.setPaint(p);
    }
    public void remove(){
    	point.remove(last);
    	repaint();
    }
   
    
    
    class MouseHandler extends MouseAdapter {

        
      
        public void mousePressed(MouseEvent e) {
        	boolean fl =true;
            Point press = new Point(e.getPoint());
            int R = 30;//radius попадания
            Graphics2D g2d = (Graphics2D) getGraphics();
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            Paint p =g2d.getPaint();
            
            BasicStroke pen1 = new BasicStroke(5);
    		g2d.setStroke(pen1);
            
    		g2d.setXORMode(xorColor);
    		
    		if(point.size() ==0){
    			
    			point.add(press);
    			last = press;
        		g2d.fillOval(e.getX()-10, e.getY()-10, 20, 20);
    		} else{
    			g2d.fillOval(e.getX()-10, e.getY()-10, 20, 20);
    			
            	for(Point p1:point){
            		
            			
            		
            			if(((Math.abs(p1.x-press.x)*Math.abs(p1.x-press.x)+Math.abs(p1.y-press.y)*Math.abs(p1.y-press.y))<R*R)){
            			g2d.setXORMode(xorColor);
            			fl = false;
            
            			}
            			
           	}
            	last = press;
            	if(fl){
            		point.add(press);
            		fl = true;
            	}
            	
            	
    		}	
    		fl = true;
            g2d.setPaintMode();
            g2d.setPaint(p);
        }

        
        public void mouseReleased(MouseEvent e) {
        	
        	 Graphics2D g2d = (Graphics2D) getGraphics();
           
             g2d.setColor(Color.BLACK);
        
          
            repaint();
          
        }

      
        public void mouseDragged(MouseEvent e) {
            
        }
    }
    
    
    public HashSet<Point> getHashSet(){
		return point;
    	
    }

}
	
	

